from vectors_lib import face_to_2d,scale_by,move_to,rotate_around_x_by_degree,combine,rotate_around_z_by_degree,to_unit_vector,rotate_around_y_by_degree
import numpy as np
import matplotlib

light = np.array([0,0,10])
camera = np.array([0,0,10])

move = move_to(1280, 720)
color_map = matplotlib.colormaps['Blues']

def filter_unvisible_faces(faces):


    # scale = scale_by(1)
    # transform = combine(rotate,scale)

    visible_faces  = []

    for face in faces:
        vx,vy,vz = face

        e1 = np.subtract(vx,vy)
        e2 = np.subtract(vx,vz)
        # 计算法向量
        normal_vector = np.cross(e1,e2)
        d = np.dot(normal_vector,camera)
        if  d > 0:

            
            # 求这个面的法线和灯光向量的点积
            # 点积是用来衡量两个向量的对齐程度(两向量夹角的大小)
            # 这里使用 1 - 是为了 color_map 输入的数值越大,颜色越深
            # 但是这里 法线和灯光向量夹角越小,点积越大,如果不用1减去的话,夹角越小,灯光越暗,不符合要求                   
            r,g,b,alpha = color_map(1- np.dot(to_unit_vector(normal_vector),to_unit_vector(light))) #3 

            
            
            color = [int(r*255),int(g*255),int(b*255),int(alpha*255)]

            visible_faces.append([np.array([vx,vy,vz]),normal_vector,color])    
    
    return visible_faces

def zip_3d_to_2d(faces):

    polygones_coordinates=[]

    for visible_face,normal_vector,color in faces :
            
        face_2d = face_to_2d(visible_face)

        coordinates =[]

        for v_2d in face_2d:
            
            v = move(v_2d)
            vx_2d,vy_2d = v
            
            coordinates.append((vx_2d,vy_2d))
        

        
        
        polygones_coordinates.append([coordinates,color])
    return polygones_coordinates        